﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuickApp;

namespace QuickApp.DbInterface
{
    public abstract class QADbTable : IQAParent, IQAChild
    {
        #region Protected Fields

        protected List<QADbColumn> columns;

        protected QADbManager dataManager;

        protected DataTable lastSelect;

        protected String name, displayName;

        protected QADbDatabase parent;

        #endregion Protected Fields

        #region Public Constructors

        public QADbTable(string name, string displayName, QADbManager dataManager, QADbDatabase parent)
        {
            this.displayName = displayName;
            this.name = name;
            this.dataManager = dataManager;
            this.parent = parent;
        }

        #endregion Public Constructors

        #region Public Properties

        public Dictionary<string, List<IQAChild>> Children
        {
            get
            {
                return new Dictionary<string, List<IQAChild>>() { { "Columns", Columns.Cast<IQAChild>().ToList() } };
            }
        }

        public List<QADbColumn> Columns
        {
            get
            {
                if (columns == null)
                    LoadColumns();
                return columns;
            }
            set { columns = value; }
        }

        public virtual string Name
        {
            get { return name; }
            private set { name = value; }
        }

        public virtual string DisplayName
        {
            get { return displayName; }
        }

        public QADbDatabase Parent
        {
            get { return parent; }
        }

        public DataTable LastSelectedTable
        {
            get { return lastSelect; }
        }

        #endregion Public Properties

        #region Public Methods

        public bool SaveLastSelect()
        {
            if (lastSelect != null)
            {
                SetDatabaseConnection();
                DataTable changes = lastSelect.GetChanges();

                foreach (DataRow dr in changes.Rows)
                {
                    if (dr.RowState == DataRowState.Deleted)
                        dataManager.Delete(GetDeleteCommand(dr));
                    if (dr.RowState == DataRowState.Modified)
                        dataManager.Update(GetUpdateCommand(dr));
                    if (dr.RowState == DataRowState.Added)
                        dataManager.Insert(GetInsertCommand(dr));
                }
                return true;
            }
            return false;
        }
        public DataTable SelectTable(int numRows = 0)
        {
            SetDatabaseConnection();
            lastSelect = dataManager.SelectTable(GetSelectCommand(numRows));
            return LastSelectedTable;
        }
        #endregion Public Methods

        #region Protected Methods

        protected abstract void LoadColumns();

        protected abstract String GetSelectCommand(int numRows = 0);
        protected abstract String GetInsertCommand(DataRow dr);
        protected abstract String GetUpdateCommand(DataRow dr);
        protected abstract String GetDeleteCommand(DataRow dr);

        protected void SetDatabaseConnection()
        {
            dataManager.ChangeCurrentDatabase(this.Parent.Name);
        }


        #endregion Protected Methods

        public override string ToString()
        {
            return DisplayName;
        }
    }
}
